package com.vanzo.encryption;

import com.vanzo.bean.Master;
import com.vanzo.talkie.Follower;
import com.vanzo.util.LogUtil;

/**
 * Created by tisen on 2018/2/27.
 */

public class Encryption {
    public static byte[] getGroupKey() {
        byte[] gk = new byte[Pdt.MAX_BUFFER_LEN];
        Integer ogkLen = new Integer(1024);
        int gkRet = Pdt.getGroupKey(gk, ogkLen);
        return ByteUtil.byteArrayToByteArray(gk, ogkLen);
    }

    public static byte[] getKillKey() {
        byte[] kk = new byte[8];
        Integer okkLen = new Integer(8);
        int kkRet = Pdt.getKillKey(kk, okkLen);
        return ByteUtil.byteArrayToByteArray(kk, okkLen);
    }

    public static byte[] getNfc2(int groupId, int terminalId, int firstId, int totalTerminalSize, int channelId, int isLocked, byte[] gk, byte[] kk) {
        byte[] nfc;
        Integer length;
        Integer result;
        nfc = new byte[Pdt.MAX_BUFFER_LEN * 4];
        length = new Integer(Pdt.MAX_BUFFER_LEN * 4);
        result = Pdt.getSuperkey2NFC(groupId, terminalId, totalTerminalSize, firstId, channelId, isLocked, gk, kk, nfc, length);
        return ByteUtil.byteArrayToByteArray(nfc, length);
    }

    public static byte[] getNfc2(Follower follower) {
        return getNfc2( follower.getGroupId(), follower.getTerminalId(),follower.getFirstId(), follower.getTotalTerminalSize(),  follower.getChannelId(), follower.getIsLocked(),follower.getGk(), follower.getKk());
    }

    public static NFC parseNFCFromByte(byte[] nfc, int length) {
        return new NFC(nfc, length);
    }

    public static byte[] getNfc2(Master master, com.vanzo.bean.Follower theNeedBindOne) {
        LogUtil.put("getNfc2", "killkey = " + ByteUtil.byteArrayToHexStr(theNeedBindOne.getKillKey()), LogUtil.WARN);
        LogUtil.put("getNfc2", "master = " + master.toString(), LogUtil.WARN);
        LogUtil.put("getNfc2", "theNeedBindOne = " + theNeedBindOne.toString(), LogUtil.WARN);
        return getNfc2( master.getGroupId(), theNeedBindOne.getTerminalId(), master.getFirstId(), master.getFollowerSize(), master.getChannelId(), master.getLocked() ? 1 : 0,master.getGk(), theNeedBindOne.getKillKey());
    }

    public static class NFC {

        private byte[] binary;
        private Integer groupId;
        private Integer terminalId;
        private Integer totalTerminalSize;
        private Integer firstTid;
        private Integer channelId;
        private Integer isLocked;
        private int result;


        public NFC(byte[] nfc, Integer nfcLen) {
            byte[] temp = new byte[Pdt.MAX_BUFFER_LEN * 2];
            Integer length = new Integer(Pdt.MAX_BUFFER_LEN * 2);
            groupId = new Integer(0);
            terminalId = new Integer(0);
            totalTerminalSize = new Integer(0);
            firstTid = new Integer(0);
            channelId = new Integer(0);
            isLocked = new Integer(0);
            result = Pdt.parseSuperkeyFromNFC(nfc, nfcLen, groupId, terminalId, totalTerminalSize, firstTid, channelId, isLocked, temp, length);
            binary = ByteUtil.byteArrayToByteArray(temp, length);
        }

        public byte[] getBinary() {
            return binary;
        }

        public Integer getLength() {
            return binary.length;
        }

        public Integer getGroupId() {
            return groupId;
        }

        public Integer getTerminalId() {
            return terminalId;
        }

        public Integer getTotalTerminalSize() {
            return totalTerminalSize;
        }

        public Integer getFirstTid() {
            return firstTid;
        }

        public Integer getChannelId() {
            return channelId;
        }

        public Integer getIsLocked() {
            return isLocked;
        }

        public int getResult() {
            return result;
        }

        @Override
        public String toString() {
            return "NFC{" +
                    "groupId=" + groupId +
                    ", terminalId=" + terminalId +
                    ", totalTerminalSize=" + totalTerminalSize +
                    ", firstTid=" + firstTid +
                    ", channelId=" + channelId +
                    ", isLocked=" + isLocked +
                    '}';
        }
    }


    public static byte[] getDeviceStatus() {
        byte[] temp = new byte[Pdt.MAX_BUFFER_LEN * 4];
        Integer length = new Integer(Pdt.MAX_BUFFER_LEN * 4);
        Pdt.getDeviceStatus(temp, length);
        return ByteUtil.byteArrayToByteArray(temp, length);
    }

    public static byte[] getLastCmdResult() {
        byte[] temp = new byte[Pdt.MAX_BUFFER_LEN * 4];
        Integer length = new Integer(Pdt.MAX_BUFFER_LEN * 4);
        Pdt.getLastCmdResult(temp, length);
        return ByteUtil.byteArrayToByteArray(temp, length);
    }

    public static byte[] sendKillMessage(int groupId, int terminalId, byte[] kk) {
        byte[] temp = new byte[Pdt.MAX_BUFFER_LEN * 4];
        Integer length = new Integer(Pdt.MAX_BUFFER_LEN * 4);
        Pdt.sendKillMessage(groupId, terminalId, kk, temp, length);
        return ByteUtil.byteArrayToByteArray(temp, length);
    }

    public static byte[] sendMessage(int groupId, int terminalId, byte[] msg) {
        byte[] temp = new byte[Pdt.MAX_BUFFER_LEN * 4];
        Integer length = new Integer(Pdt.MAX_BUFFER_LEN * 4);
        Pdt.sendMessage(groupId, terminalId, msg, temp, length);
        return ByteUtil.byteArrayToByteArray(temp, length);
    }

    public static class Binary {

        private byte[] type;
        private Short length;
        private byte[] value;

        public Binary(byte[] binary) {
            type = new byte[Pdt.MAX_BUFFER_LEN];
            length = new Short((short) 0);
            value = new byte[2];
            Pdt.parseBinary(binary, binary.length, type, length, value);
        }
    }
}
